import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class Arrow extends CustomPainter {
  Color color;
  ArrowDirection direction;

  Arrow({this.color = Colors.white, this.direction = ArrowDirection.LEFT});

  /// 画图的笔
  Paint _painter = Paint()
    ..strokeWidth = 1.2
    ..style = PaintingStyle.fill;

  @override
  void paint(Canvas canvas, Size size) {
    // 超过父组件的绘制内容都会被剪切掉
    Rect rect = Offset.zero & size;
    canvas.clipRect(rect);

    _drawArrow(canvas, size);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return true;
  }

  _drawArrow(Canvas canvas, Size size) {
    _painter.color = color;
    Path path = Path();
    if (direction == ArrowDirection.LEFT) {
      path.moveTo(size.width, 0);
      path.lineTo(0, size.height / 2);
      path.lineTo(size.width, size.height);
      path.close();
    } else if (direction == ArrowDirection.RIGHT) {
      path.moveTo(0, 0);
      path.lineTo(size.width, size.height / 2);
      path.lineTo(0, size.height);
      path.close();
    } else if (direction == ArrowDirection.DOWN) {
      path.moveTo(0, 0);
      path.lineTo(size.width / 2, size.height);
      path.lineTo(size.width, 0);
      path.close();
    } else if (direction == ArrowDirection.UP) {
      path.moveTo(0, size.height);
      path.lineTo(size.width / 2, 0);
      path.lineTo(size.width, size.height);
      path.close();
    }
    canvas.drawPath(path, _painter);
  }
}

enum ArrowDirection {
  LEFT,
  RIGHT,
  UP,
  DOWN,
}
